\input tibex
\input pixidxfile
%排版藏文傳統長函書的包
\parindent=0pt%
%
\def\rrot@te#1{\setbox0=\hbox{#1}%
  \special{x:gsave}\special{x:rotate 270}%rotate
  \box0 \special{x:grestore}}
\def\lrot@te#1{\setbox0=\hbox{#1}%
  \special{x:gsave}\special{x:rotate 90}%rotate
  \box0 \special{x:grestore}}
%
\NoInterLetterSkip
%
\newdimen\dimn%計算hoffset要用的臨時變量
\def\setpaperhalfa#1{%設置紙張大小，半A4/三分之一A3
  \ifnum#1=4 % 1/2 A4
     \pdfpageheight90mm\pdfpagewidth297mm%
     \vsize=55mm\hsize=243mm%
     \voffset=-10mm
  \else%
     \ifnum#1=3 % 1/3 A3
       \pdfpageheight99mm\pdfpagewidth420mm%
       \vsize=66mm\hsize=366mm%
       \voffset=-10.5mm%
     \fi\fi%
  \dimn=\pdfpagewidth \advance\dimn by -\hsize%
  \advance\dimn by -28mm \divide\dimn by 2 %
  \advance\dimn by -11mm %
  \hoffset=\dimn %
}%
\setpaperhalfa4%缺省的將紙張設爲半A4
%
\maxdepth=0pt%
\nopagenumbers%
% 這些與字體相關，應該定義一個宏讓用戶自己設缺省行高
\baselineskip=10.5mm plus1mm%
\lineskip=0mm plus1mm%
\lineskiplimit=0.2mm%
%
\parskip=0pt \parfillskip=0pt%
%
\newdimen\pb@xwidth \newdimen\pb@xheight \newdimen\ruleht@%
\newdimen\upm@rgin \upm@rgin=0pt%上邊框與文字的邊距
\newdimen\tmp@dim %
%\setboxupmargin設置上文字邊距
\def\setboxupmargin#1{\upm@rgin=#1}
% 變量初始化宏，在\output裡調用
\def\setboxsize{%
  \pb@xwidth=\hsize \pb@xheight=\vsize \ruleht@=1pt %
  \advance\pb@xwidth by 18pt \advance\pb@xwidth by 4.6mm %
  \advance\pb@xheight by 12pt \advance\pb@xheight by \upm@rgin \advance\pb@xheight by \upm@rgin %
  \advance\pb@xheight by \maxdepth }%
\def\dr@wpbox{%用ruleht@中所指定的線寬畫pb@xwidth*pb@xheight方框
  \hrule height\ruleht@ width\pb@xwidth %上横边
  \hbox{%
    \vrule width\ruleht@ height\pb@xheight%
    \kern-\ruleht@ \kern\pb@xwidth \kern-\ruleht@%
    \vrule width\ruleht@ height\pb@xheight}%
  \hrule height\ruleht@ width\pb@xwidth %下横边
  }%
\def\shrinkpb@x{%将长宽减去2倍的ruleht@
  \advance\pb@xwidth by-\ruleht@ \advance\pb@xwidth by-\ruleht@ %
  \advance\pb@xheight by-\ruleht@ \advance\pb@xheight by-\ruleht@}%
\def\dblvrule#1{%雙竪線間隔2pt
  \vrule width1pt height#1%
  \kern2pt%
  \vrule width1pt height#1}%
\def\pdblvlin@{
  \dblvrule\pb@xheight}
% 由使用者设置这个变量告诉TibeX在最后一葉的正面加上་བྱོན
\newcount\totalpages \totalpages=0%
%
\def\putp@geno{\c@m=\pageno \advance\c@m by1 \divide\c@m by2 \tibpageno\c@m}
\def\tibpageno{\tibnumber} %設置頁碼風格，這裏是傳統的གཅིག་གཉིས་གསུམ།風格頁碼
\def\leftp@gehdr{%奇数页左边页眉框中的文字
    \tmp@dim=\pb@xheight \advance\tmp@dim by -12pt %
    \vbox to \tmp@dim{\hbox to 0pt{\rrot@te{\hbox to \tmp@dim{ %左頁眉
        \hdr_up\pre_phdr \hfil%
        \putp@geno %輸出頁碼
        \c@m=\totalpages %
        \ifnum\pageno>\c@m \relax%
        \else%
            \advance\c@m by-\pageno%
            \ifnum\c@m<2 ་བྱོན། \fi\fi %加在最後一頁
        \hfil\post_phdr\kern12pt}}%
    \hss}\vss} %左頁眉
}%
% 控制是否加邊框及頁眉
\newif\ifframe \framefalse % 是否加最外邊框
\newif\ifinnerframe \innerframetrue % 是否不加内框
\def\noinnerframe{ % 不加内框，祇加上左頁眉並留空
    \innerframefalse %
    }
\newif\ifmultifr@me \multifr@mefalse % 臨時變量，是否是奇數頁加三内框
%
\def\setppix#1{\XeTeXpicfile#1 height \pb@xheight}
\def\pboxlpic{} %設置這兩個宏來讓\output輸出圖片
\def\pboxrpic{} %
%中間的正文框，\ifpixbox則給兩邊加上圖片框
\newdimen\pixb@xwidth
\def\pb@xpic#1{
  \vbox to \pb@xheight{
    \pixb@xwidth=\pb@xheight %
    \multiply\pixb@xwidth by3 \divide\pixb@xwidth by4% 寬=3/4高
    \hbox to \pixb@xwidth{#1\hss}}}%
\def\midtextb@x{ %
  \ifpixbox %
    %\message{ pixpageno=\the\pixp@geno. } %
    \ifnum\pageno=\pixp@geno %
      \hrule height\ruleht@ width\pb@xwidth %上横边
      \hbox to \pb@xwidth{%
        \vrule width\ruleht@ height\pb@xheight%左豎邊
        %\kern-\ruleht@ \kern\pb@xwidth \kern-\ruleht@%
        %\show\pboxlpic %
        \pb@xpic{\pboxlpic} %左圖片
        \pdblvlin@ %雙竪綫
        %\kern-\pixb@xwidth \kern-pixb@xwidth \kern-8pt%中空
        \hfil %
        \pdblvlin@ %雙竪綫
        \pb@xpic{\pboxrpic} %右圖片
        \vrule width\ruleht@ height\pb@xheight} %右豎邊
      \hrule height\ruleht@ width\pb@xwidth %下横边
    \else % 這一頁沒圖片
      %\message{===no. \the\pageno{\the\pixp@geno} is not pix page, .} %
      \dr@wpbox \fi % \ifnum
  \else % not pixbox
    %\message{===no pixbox.} %
    \dr@wpbox %
  \fi} %\ifpixbox
\def\estim@tepixboxwidth{ %預估\pixboxwidth的值,在setparshape中要用到
  \pixb@xwidth=\vsize %
  \advance\pixb@xwidth by 27pt %
  \advance\pixb@xwidth by-6pt \advance\pixb@xwidth by\maxdepth %\shrinkbox，+2pt之後的内框高
  \multiply\pixb@xwidth by 3 \divide\pixb@xwidth by 4 % 3/4
}
\def\RightNumericPageno{\null} % 缺省的不加右邊的數字頁碼
%
% 當需要在頁面右邊加阿拉伯數字頁碼時加上如下的定義
%\def\RightNumericPageno{ %
%    \ifframe \vskip-35mm \else \vskip -45mm \fi % P1-P4 與 P5以後的居中位移不同
%    \smash { % 阿拉伯數字頁碼
%        \hbox {\hskip\hsize \hskip17mm\lrot@te{\bigtenrm\folio} \hss} } % smash
%} %
%
% Output routine
%
\def\footline{\hss\null\hss}
\def\onepageout#1{%
 \shipout\vbox{%
  %\message{ BG[pixpageno=\the\pixp@geno.] }
  \ifframe%方框
    \setboxsize %初始化計算方框大小的寄存器
    \offinterlineskip %
    \vskip-4pt \vskip-\upm@rgin %
    \hbox{\kern-16pt\vbox to 4pt{ %粗外框
       \ruleht@=2pt \dr@wpbox %
       \vss}}%
    \ruleht@=4pt \shrinkpb@x \advance\pb@xheight by2pt%
    \ifinnerframe % 需要加内框
        \ifodd\pageno \multifr@metrue %
        \else\ifnum\pageno=2 \multifr@metrue %特例，用於P2不想用四重方框時
        \else\multifr@mefalse\fi\fi %
        %
        \ifmultifr@me % 奇數頁三内框
            \ruleht@=1pt %
            \hbox{ % 内含三个框
                \kern-12.8pt %
                \vbox to 2pt{% 左小框
                    \hrule width36pt height\ruleht@%
                    \hbox to 36pt{%
                        \vrule width\ruleht@ height\pb@xheight%
                        \hfil\kern15pt\leftp@gehdr\hfil % 左页眉文字
                        \vrule width\ruleht@ height\pb@xheight%
                    }%
                    \hrule width36pt height\ruleht@ \vss} %
                \advance\pb@xwidth by-36pt %
                \advance\pb@xwidth by-36pt % 算中框的寬度
                \kern1pt%
                \vbox to 2pt{% 正文框
                    \advance\pb@xwidth by -3.5pt \midtextb@x \vss} %
                \kern1.25pt %
                \vbox to 2pt{% 右小框
                    \pb@xwidth=36pt \dr@wpbox \vss} %
            } %\hbox
        \else % 偶數頁套细方框
            \hbox{\kern-12pt\vbox to 2pt{
               \ruleht@=1pt \dr@wpbox %
               \vss}} \fi %\ifmultifr@me
    \else %不需要加内框
        \ifodd\pageno % 奇數頁P5等加頁眉
            \hbox{ %
                \kern-12.8pt %
                \vbox to 2pt{% 頁眉
                    \hbox to 36pt{%
                        \hfil\kern10pt\leftp@gehdr\hfil % 左页眉文字
                    }\vss}} %
        \else % 偶數頁
            \hbox{\kern-12.8pt\vbox to 2pt{\vss}} % 調整奇偶頁對齊
        \fi %\ifodd
    \fi %\ifinnerframe 
  \fi %\ifframe
  \vskip2pt \vskip\upm@rgin %
  \vbox to \vsize{ #1 \boxmaxdepth=\maxdepth } % 页面内容
  \RightNumericPageno % 右邊的數字頁碼
 } % shipout\vbox
  \nextp@gepixs % 讀入下一頁圖片索引
  %\message{ [pixpageno=\the\pixp@geno.] }
  %\showpixvars %
  \advancepageno %
}%
%设置为输出宏
\output{\onepageout{\unvbox255}}
%
% 用於P1,P2,P3的四層方框
%
\newif\ifpagehdr\pagehdrfalse %是否在四层方框中放左页眉
%\newdimen\b@xwidth %四层方框宽
%\newdimen\b@xheight %高
\newcount\quadboxgapstyle \quadboxgapstyle=1
\newif\ifpixbox\pixboxfalse %是否左右留圖片框 %普通四層框
\newdimen\d@r %传递dimension的临时寄存器
\newdimen\d@t %存放標題兩邊的長框寬度的臨時寄存器
\def\setboxwidth#1{\pb@xwidth=#1} %设置方框的宽度
\def\setboxheight#1{\pb@xheight=#1} %设置高度
\def\shrinkb@xsize#1#2{%将宽、高分别减少两倍#1 #2
  \advance\pb@xwidth by-#1 \advance\pb@xwidth by-#1 %
  \advance\pb@xheight by-#2 \advance\pb@xheight by-#2 %
  }%
\def\calcinnerb@xsize{%根據\ifpixbox來設置二三層框間距 \d@r回傳橫移間距
  \ifpixbox % 帶圖片的四層框
      \d@r=1.6pc \d@t=1.6pc%
      \shrinkb@xsize{1.6pc}{1.6pc} %
      \vskip 1.6pc
  \else % 普通四層框
      \ifnum\quadboxgapstyle=1 %風格1，二、三框間上下空1/5
          \d@r=\pb@xheight \divide\d@r by 5 \d@t=\d@r %
          \advance\d@r by1pt %
          \shrinkb@xsize{\d@r}{\d@r} %
          \vskip \d@r
      \else\ifnum\quadboxgapstyle=2 
          \d@r=27pt \d@t=27pt% 二三層間小間距
          \shrinkb@xsize{27pt}{12pt}
          \vskip 12pt
      \fi\fi\fi%
  }%
\def\dblvlin@{%
  \dblvrule\pb@xheight}%
\def\dr@wbox#1{%用#1中所指定的線寬畫方框
  \hrule height#1 width\pb@xwidth %上横边1
  \hbox{%
    \vrule width#1 height\pb@xheight%
    \kern-#1 \kern\pb@xwidth \kern-#1%
    \vrule width#1 height\pb@xheight}%
  \hrule height#1 width\pb@xwidth %下横边1
  }%
%
\def\hdr_up{} %調節頁眉位置，用\kern控制頁眉上下
\def\pre_phdr{} %頁碼前頁眉
\def\post_phdr{} %頁碼後頁眉
\newdimen\hdroffs@t \hdroffs@t=0pt
\def\putp@gehdr{%放頁眉，用於P2,P3
  \dimn=\pb@xheight \advance\dimn by-.25em %
  \hdroffs@t=-\dimn \divide\hdroffs@t by 2 %
  \vbox to 2cm{\vskip\hdroffs@t \hdr_up \hbox to 0pt{%
    \rrot@te{\hbox to \dimn{%
      \pre_phdr\hfil%
      \putp@geno%
      \hfil\post_phdr}}\hss}\vss}}%
\def\lmultivrul@s{%左邊第四方框內的幾條竪線
  \d@t=\pb@xheight \divide\d@t by 8 \multiply\d@t by 3 % 高:寬=4:3
  \kern\d@t % 3/8H
  %\kern1.2pc \ifpagehdr\putp@gehdr\fi \kern-1.2pc
  \kern\d@t % 3/8H
  \dblvlin@ %4pt
  \multiply\d@t by2 \divide\d@t by6 \kern\d@t %1/8w+4pt
  \dblvlin@ 
  }%
\def\rmultivrul@s{%右邊第四方框內的幾條竪線
  \dblvlin@ %
  \d@t=\pb@xheight \divide\d@t by 8 \kern\d@t % 1/8 H 
  \dblvlin@ %
  \multiply\d@t by 6 \kern\d@t % 3/4 H
  }%
\def\rem@inedwidth#1{%計算剩下的寬度之半，存在\d@r中
  \d@t=\pb@xheight \divide\d@t by 8 \multiply\d@t by 7 % 3/4 + 1/8 = 7/8H
  \d@r=\pb@xwidth \advance\d@r by-\d@t \advance\d@r by-\d@t %
  \advance\d@r by-18pt %
  \setbox0=#1 %
  \advance\d@r by-\wd0 %文字的寬度
  \advance\d@r by-.7pt % 調整對齊
  \divide\d@r by 2}%
\def\setpixboxwidth{ %計算圖片框寬度放在\dt中
  \d@t=\pb@xheight \multiply\d@t by3 \divide\d@t by4 }%
\def\rem@inedwidthpic{ %計算剩下的寬度存在d@r中
  \d@r=\pb@xwidth \advance\d@r by-2.8pc %左第一框
  \advance\d@r by -4pt %雙竪綫
  \setpixboxwidth \multiply\d@t by2 %兩個圖片框
  \advance\d@r by -\d@t %
  \advance\d@r by -4pt \advance\d@r by -4pt %左右各有一個雙竪綫
  \advance\d@r by -2.7pt}%
%
\newif\ifnopixb@x %
\def\quadr@box#1{%四層方框的主宏
  \offinterlineskip%
  \hbox{\vbox to 4pt{%第一方框
    \dr@wbox{2pt}%
    \vss}}%
  \shrinkb@xsize{4pt}{4pt}%
  \advance\pb@xheight by 2pt%
  \hbox{\kern4pt\vbox to 1pt{%第二方框
    \dr@wbox{1pt}%
  \vss}}%
  \kern1.2pc % 頁碼
    \ifpagehdr \vbox to 0pt{\vskip 2.9cm\hbox to 0pt{ %
      \hskip 2pc\putp@gehdr\hss}\vss}\fi %
  \kern-1.2pc %
  \calcinnerb@xsize %設置二三層框之間的間距
  \hbox{\kern5pt\kern\d@r%
    \vbox to 3pt{%第三方框
      \dr@wbox{1pt}%
      \vss}}%
  \shrinkb@xsize{3pt}{3pt}%
  \hbox{\kern8pt\kern\d@r\vbox{%第四方框
    \hrule height1pt width\pb@xwidth %上横边4
    \hbox to\pb@xwidth{%
      \vrule width1pt height\pb@xheight % 1pt
      \hbox{%
          \ifpixbox %
              \ifnum\pageno=\pixp@geno \nopixb@xfalse %
              \else \nopixb@xtrue \fi %
          \else \nopixb@xtrue \fi %
          \ifnopixb@x %普通四層框
          %\message{'pageno=\the\pageno, pixp@ageno=\the\pixp@geno'}
              \lmultivrul@s %
              %\tracingcommands=3 \tracingmacros=3
              \rem@inedwidth{#1} %
              \hskip\d@r \hfil %
              \vbox{\vfil \box0 \vskip7pt\vfil}% 
              \hfil \hskip\d@r %
              \rmultivrul@s %
          \else %左右加圖片框
          %\message{'pageno=\the\pageno, pixp@ageno=\the\pixp@geno'}
              \hskip1.8pc \ifpagehdr\putp@gehdr\fi \hskip1pc %頁眉
              \dblvlin@ %左第一框帶頁眉
              \setpixboxwidth %計算圖片框寬度
              \vbox to \pb@xheight{\hbox to \d@t{\pboxlpic\hss}} %
              %\kern-.7pt
              \dblvlin@ %左圖片框
              \rem@inedwidthpic \kern\d@r \kern-1.4pt%中間文字留空
              \dblvlin@ \setpixboxwidth %右圖片框
              \vbox to \pb@xheight{\hbox to \d@t{\pboxrpic\hss}} %\kern-.7pt %
          \fi}%
      \vrule width1pt height\pb@xheight\hss}%
    \hrule height1pt width\pb@xwidth %下横边4
    }}%
}%
%
\newcount\t@mpmod
\def\modulo#1#2{ % 求余数
    \t@mpmod=#1 %
    \divide\t@mpmod by#2 \multiply\t@mpmod by#2 \multiply\t@mpmod by-1 %
    \advance\t@mpmod by #1}
%用于设置从P5开始的正文段落形状的宏
\newdimen\@ddlskip \@ddlskip=0pt
\newdimen\@venlskip \@venlskip=0pt
\newdimen\@ddtexthsize % 用來計算奇數頁文字寬度
\newdimen\ev@ntexthsize % 用來計算偶數頁文字寬度
\def\slin@il{\@ddlskip\@ddtexthsize}% short line indent length
\def\twoslin@il{\slin@il\slin@il}%兩個短行
\def\threeslin@il{\twoslin@il\slin@il}%
\def\fourslin@il{\twoslin@il\twoslin@il}%
\def\fiveslin@il{\threeslin@il\twoslin@il}%
\def\sixslin@il{\threeslin@il\threeslin@il}%
\def\sevenslin@il{\fourslin@il\threeslin@il}%最多一頁七個短行
\def\llin@il{\@venlskip\ev@ntexthsize}%
\def\twollin@il{\llin@il\llin@il}%
\def\threellin@il{\twollin@il\llin@il}%
\def\fourllin@il{\twollin@il\twollin@il}%
\def\fivellin@il{\threellin@il\twollin@il}%
\def\sixllin@il{\threellin@il\threellin@il}%
\def\sevenllin@il{\fourllin@il\threellin@il}%最多一頁七個長行
\def\pfiv@{\fiveslin@il \fivellin@il}%
\def\fourpfiv@{\pfiv@\pfiv@\pfiv@\pfiv@}%
\def\sixteenpfiv@{\fourpfiv@\fourpfiv@\fourpfiv@\fourpfiv@}%
\def\thirtytwopfiv@{\sixteenpfiv@\sixteenpfiv@}%
\def\p@six{\sixslin@il \sixllin@il}%
\def\f@urpsix{\p@six\p@six\p@six\p@six}%
\def\sixte@npsix{\f@urpsix\f@urpsix\f@urpsix\f@urpsix}%
\def\thirtytw@psix{\sixte@npsix\sixte@npsix}%
\def\psev@n{\sevenslin@il\sevenllin@il}
\def\fourpsev@n{\psev@n\psev@n\psev@n\psev@n}%
\def\sixteenpsev@n{\fourpsev@n\fourpsev@n\fourpsev@n\fourpsev@n}%
\def\thirtytwopsev@n{\sixteenpsev@n\sixteenpsev@n}%
\def\setparshape#1#2{%{#1=5/6/7}{#2=该段文字大致最多的页数(<32*2)}
    %设置该段文字为5/6/7個短行，接5/6/7個長行
    \prevlinesrem@ined=0
    \@ddtexthsize=\hsize %
    \ev@ntexthsize=\hsize \advance\ev@ntexthsize by 4pt %
    \ifinnerframe % 帶内框，外框已經左移了12pt(0.42cm)
        \advance\@ddtexthsize by-2.4cm % 帶内框時P5左右各空1.2cm(36pt)
        \@ddlskip=35pt \@venlskip=-2pt % P6左右皆不留空
        \ifpixbox % 内框左右有圖片
            \estim@tepixboxwidth % 預估圖片框寬度，設置\pixb@xwidth
            \advance\@ddtexthsize by -\pixb@xwidth %
            \advance\@ddtexthsize by -\pixb@xwidth %
            \advance\@ddtexthsize by -4mm %
            \advance\@ddlskip by \pixb@xwidth %
            \advance\@ddlskip by 2mm %
            %\message{"oddlskip=\the\@ddlskip."}
        \fi
    \else % 不帶内框，排版完成後再嵌入僅含有左右黑條的P45.pdf
        \advance\@ddtexthsize by -21.47mm % 不帶内框時P5左19.95mm右9mm
        %\showthe\@ddtexthsize %
        \@ddlskip=45.3pt \@venlskip=16.6pt % P5 19.95mm(57pt) / P6 0.9cm(26pt)
        \advance\ev@ntexthsize by-12.76mm % P6左右黑邊合計
        %\showthe\ev@ntexthsize %
    \fi % \ifinnerframe
    \ifnum#1=5 % 設置n頁的段落形狀
        \ifnum#2<5 \parshape=40 \fourpfiv@ %4*2頁
        \else\ifnum#2<17 \parshape=160 \sixteenpfiv@ %16*2頁
        \else \parshape=320 \thirtytwopfiv@ \fi\fi %最多不超過32*2頁
    \else\ifnum#1=6 %
        \ifnum#2<5 \parshape=48 \f@urpsix %
        \else\ifnum#2<17 \parshape=192 \sixte@npsix %
        \else \parshape=384 \thirtytw@psix \fi\fi %
    \else\ifnum#1=7 %
        \ifnum#2<5 \parshape=56 \fourpsev@n %
        \else\ifnum#2<17 \parshape=224 \sixteenpsev@n %
        \else \parshape=448 \thirtytwopsev@n \fi\fi %
    \fi\fi\fi} %
\def\setrestedparsh@pe#1#2{ %{5/6/7}{一個正反面中還剩下的行數}祇考虑一段2*16頁
    %\message{arg1=#1, arg2=#2}
    \ifnum#1=5 %
        \ifcase#2 \parshape=160 \sixteenpsev@n \relax %
        \or \parshape=161 \llin@il \sixteenpfiv@ %1行
        \or \parshape=162 \twollin@il \sixteenpfiv@ %2行
        \or \parshape=163 \threellin@il \sixteenpfiv@ %3行
        \or \parshape=164 \fourllin@il \sixteenpfiv@ %4行
        \or \parshape=165 \fivellin@il \sixteenpfiv@ %5行
        \or \parshape=166 \slin@il\fivellin@il \sixteenpfiv@ %1+5行
        \or \parshape=167 \twoslin@il\fivellin@il \sixteenpfiv@ %2+5行
        \or \parshape=168 \threeslin@il\fivellin@il \sixteenpfiv@ %3+5行
        \or \parshape=169 \fourslin@il\fivellin@il \sixteenpfiv@ %4+5行
        \or \parshape=160 \sixteenpfiv@ \fi %5+5
    \else\ifnum#1=6 %
        \ifcase#2 \parshape=192 \sixte@npsix \relax %
        \or \parshape=193 \llin@il \sixte@npsix %1
        \or \parshape=194 \twollin@il \sixte@npsix %2
        \or \parshape=195 \threellin@il \sixte@npsix %3
        \or \parshape=196 \fourllin@il \sixte@npsix %4
        \or \parshape=197 \fivellin@il \sixte@npsix %5
        \or \parshape=198 \sixllin@il \sixte@npsix %6
        \or \parshape=199 \slin@il\sixllin@il \sixte@npsix %1+6
        \or \parshape=200 \twoslin@il\sixllin@il \sixte@npsix %2+6
        \or \parshape=201 \threeslin@il\sixllin@il \sixte@npsix %3+6
        \or \parshape=202 \fourslin@il\sixllin@il \sixte@npsix %4+6
        \or \parshape=203 \fiveslin@il\sixllin@il \sixte@npsix %5+6
        \or \parshape=192 \sixte@npsix \fi %6+6 
    \else\ifnum#1=7 %
        \ifcase#2 \parshape=224 \sixteenpsev@n \relax %
        \or \parshape=225 \llin@il \sixteenpsev@n %前段剩1行
        \or \parshape=226 \twollin@il \sixteenpsev@n %
        \or \parshape=227 \threellin@il \sixteenpsev@n %
        \or \parshape=228 \fourllin@il \sixteenpsev@n %
        \or \parshape=229 \fivellin@il \sixteenpsev@n %
        \or \parshape=230 \sixllin@il \sixteenpsev@n %
        \or \parshape=231 \sevenllin@il \sixteenpsev@n %
        \or \parshape=232 \slin@il\sevenllin@il \sixteenpsev@n %1+7
        \or \parshape=233 \twoslin@il\sevenllin@il \sixteenpsev@n %
        \or \parshape=234 \threeslin@il\sevenllin@il \sixteenpsev@n %
        \or \parshape=235 \fourslin@il\sevenllin@il \sixteenpsev@n %
        \or \parshape=236 \fiveslin@il\sevenllin@il \sixteenpsev@n %
        \or \parshape=237 \sixslin@il\sevenllin@il \sixteenpsev@n %
        \or \parshape=224 \sixteenpsev@n \relax \fi %
    \fi\fi\fi} %
\newcount\prevlinesrem@ined \prevlinesrem@ined=0 %上一段所剩最後一頁中行
\newcount\t@i \newcount\t@j % 臨時變量
\def\setnewparshape#1{ %{5/6/7}根據上一段剩餘的行數設置新一段的parshape
    %\message@!\the\prevlinesrem@ined}
    \t@j=#1 \multiply\t@j by 2 %
    \t@i=\prevgraf \advance\t@i by \prevlinesrem@ined %加上上一段剩下的行數
    %\showthe\t@i \showthe\t@j %
    \modulo{\the\t@i}{\the\t@j} %計算本段開始時已排滿的行數
    \prevlinesrem@ined=\t@mpmod %更新\prevlinesrem@ined下一段要用
    \advance\t@j by -\t@mpmod % 一葉中總行數減去已排滿的行數
    %\showthe\t@j \s@owthe\prevlinesrem@ined %
    \setrestedparsh@pe{#1}{\the\t@j}}
%某些書（如一些曡字咒文）行高過大，需要臨時調整某些頁為少一行時，用下面兩個宏來强制斷頁
\def\tcnewoddpage#1{\eject\setparshape#1{16}\TibPageLines=#1}%
\def\tcnewevenpage#1{\eject\setrestedparsh@pe{#1}{#1}}%
%簡單的單層標題方框
\def\titlebox#1{\hbox{\vbox{
\hrule height1pt\hbox{\vrule width1pt\kern4pc\vrule width1pt
\vbox{\kern9pt#1\kern9pt}\vrule width1pt\kern4pc\vrule width1pt}\hrule height1pt}
}}
%用於標題的多重外框 
\def\DblFrameTitleBox#1#2#3{\null\vfil%控制上下居中
  \vskip#1 %控制框中文字的上下位置
  \vbox{ %
    \hbox{\kern#2 %kern参数控制居中
    \vbox{\quadr@box{\hbox{\Sam #3}}}} %標題文字
  }
  \vfil\null%控制上下居中
}
%用於P2、P3的多重外框 
\def\PageFrameDblBox#1#2#3{ %畫頁邊框的宏，三個參數{#1控制邊框左右}{#2上下}{控制其後文字上下位置}
\hbox{\kern-3.5pc\kern#1 %kern参数控制居中
  \vbox to 0pt{%
    \vskip#2 %
    \quadr@box{\null} %
    \vss}}\nointerlineskip%
  \vskip#3 %控制第一行文字的位置正好在框內
}
%設置自動加在奇數頁首的雲頭符
\newbox\pghdr%
\TibHeaderBox=\pghdr%
\def\header{{\SamChe ༄༅\kern.01pt།\hskip2.7em plus .5em minus.5em།}}%
\setbox\pghdr=\hbox{\header}%
%用來給文檔最后加上空白頁
\def\blankpage{\line{}\vfil\eject}%
\Sam
